package cn.guetzjb.sys.service.impl;

import cn.guetzjb.security.utils.SecurityUtils;
import cn.guetzjb.sys.domain.SysDept;
import cn.guetzjb.sys.domain.SysRole;
import cn.guetzjb.sys.domain.SysUser;
import cn.guetzjb.sys.domain.SysUserRole;
import cn.guetzjb.sys.service.SysUserRoleService;
import cn.guetzjb.sys.service.SysUserService;
import cn.guetzjb.sys.mapper.SysUserMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author admin
 * @description 针对表【sys_user(用户信息表)】的数据库操作Service实现
 * @createDate 2023-12-17 17:20:25
 */
@Service
@RequiredArgsConstructor
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
        implements SysUserService {

    private final SysUserRoleService userRoleService;

    @Override
    public SysUser getRoleListByUsername(String username) {
        MPJLambdaWrapper<SysUser> wrapper = createWrappers();
        wrapper.eq(SysUser::getUserName, username);
        return baseMapper.selectJoinOne(SysUser.class, wrapper);
    }

    @Override
    public SysUser getUserById(Long userId) {
        MPJLambdaWrapper<SysUser> wrapper = createWrappers();
        wrapper.eq(SysUser::getUserId, userId);
        return baseMapper.selectJoinOne(SysUser.class, wrapper);
    }

    @Override
    public List<SysUser> getUserAll() {
        MPJLambdaWrapper<SysUser> wrapper = createWrappers();
        return baseMapper.selectJoinList(SysUser.class, wrapper);
    }

    @Override
    public SysUser registerUser(String username, String password) {
        SysUser user = new SysUser();
        user.setUserName(username);
        user.setPassword(SecurityUtils.encryptPassword(password));
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        user.setNickName(username);
        user.setRemark("用户");
        //默认分配此部门
        user.setDeptId(100L);
        baseMapper.insert(user);
        //关联用户角色表
        userRoleService.save(new SysUserRole(user.getUserId(), 2L));
        return user;
    }

    @Override
    public boolean hasUser(String username) {
        Long cnt = baseMapper.selectCount(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUserName, username));
        return cnt != 0;
    }

    private MPJLambdaWrapper<SysUser> createWrappers() {
        MPJLambdaWrapper<SysUser> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(SysUser.class)
                .leftJoin(SysUserRole.class, SysUserRole::getUserId, SysUser::getUserId)
                .leftJoin(SysRole.class, SysRole::getRoleId, SysUserRole::getRoleId)
                .leftJoin(SysDept.class, SysDept::getDeptId, SysUser::getDeptId)
                .selectCollection(SysRole.class, SysUser::getRoles)
                .selectCollection(SysRole.class, SysUser::getRoleIds, map -> map.result(SysRole::getRoleId))
                .selectAssociation(SysDept.class, SysUser::getDept);
        return wrapper;
    }
}




